package dj.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dj.dao.BaseDao;
import dj.model.LoginInfo;
import dj.pageModel.Puser;
import dj.service.LoginInfoService;
import dj.utils.DateUtils;

@Service("loginInfoService")
public class LoginInfoServiceImpl implements LoginInfoService {

	private BaseDao<LoginInfo> loginInfoDao;

	public BaseDao<LoginInfo> getLoginInfoDao() {
		return loginInfoDao;
	}

	@Autowired
	public void setLoginInfoDao(BaseDao<LoginInfo> loginInfoDao) {
		this.loginInfoDao = loginInfoDao;
	}

	public void saveLoginInfo(Puser user) {
		LoginInfo li = new LoginInfo();
		li.setId(UUID.randomUUID().toString());
		li.setLogintime(new Timestamp(new Date().getTime()));
		li.setLoginip(user.getIp());
		li.setUserid(user.getId());
		li.setLogtype("login");
		loginInfoDao.save(li);
	}

	public void saveLogoutInfo(Puser user) {
		LoginInfo li = new LoginInfo();
		li.setId(UUID.randomUUID().toString());
		li.setLogintime(new Timestamp(new Date().getTime()));
		li.setLoginip(user.getIp());
		li.setUserid(user.getId());
		li.setLogtype("logout");
		loginInfoDao.save(li);
	}

	public List<LoginInfo> getLoginInfoPagination() {
		String p = ServletActionContext.getRequest().getParameter("page");
		String r = ServletActionContext.getRequest().getParameter("rows");
		int page = 0;
		int rows = 0;
		try {
			page = Integer.parseInt(p);
			rows = Integer.parseInt(r);
		} catch (NumberFormatException e1) {
			e1.printStackTrace();
		}
		String startDate = ServletActionContext.getRequest().getParameter("startDate");
		String endDate = ServletActionContext.getRequest().getParameter("endDate");

		String hql = "from LoginInfo where 1=1 ";
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != startDate && !"".equals(startDate)) {
			hql += "and logintime>:startDate ";
			params.put("startDate", DateUtils.toDate(startDate, DateUtils.DEFAULT_DATE_PATTERN));
		}
		if (null != endDate && !"".equals(endDate)) {
			hql += "and logintime<:endDate ";
			params.put("endDate", DateUtils.toDate(endDate, DateUtils.DEFAULT_DATE_PATTERN));
		}
		hql += "order by logintime desc";

		return loginInfoDao.find(hql, params, page, rows);
	}

	public Long getLoginInfoNum() {
		String startDate = ServletActionContext.getRequest().getParameter("startDate");
		String endDate = ServletActionContext.getRequest().getParameter("endDate");

		String hql = "select count(o) from LoginInfo o where 1=1 ";
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != startDate && !"".equals(startDate)) {
			hql += "and logintime>:startDate ";
			params.put("startDate", DateUtils.toDate(startDate, DateUtils.DEFAULT_DATE_PATTERN));
		}
		if (null != endDate && !"".equals(endDate)) {
			hql += "and logintime<:endDate ";
			params.put("endDate", DateUtils.toDate(endDate, DateUtils.DEFAULT_DATE_PATTERN));
		}
		return loginInfoDao.getCount(hql, params);
	}

}
